﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BussinessLayer;
using MyCommon;

namespace QLBaoHanh
{
    public partial class frmLogin : Form
    {
        public frmMain _parentForm;
        private bool _isLook;
        public frmLogin()
        {
            InitializeComponent();
            _isLook = false;
        }

        public void SetUsernameTextbox(string inText, bool inReadOnly)
        {
            txtUsername.Text = inText;
            txtUsername.ReadOnly = inReadOnly;
        }

        public void FocusOnTxtPwd()
        {
            txtPwd.Focus();
        }

        public void SetIsLook(bool inIsLook)
        {
            _isLook = inIsLook;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {

            if (_parentForm != null)
            {
                _parentForm.ExitApp();
                this.Close();
            }
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string Username = txtUsername.Text;
            string Pwd = txtPwd.Text;
            if (Username == "")
            {
                lblError.Text = "Phải nhập tên đăng nhập";
                lblError.Visible = true;
            }
            else if (Pwd == "")
            {
                lblError.Text = "Phải nhập mật khẩu";
                lblError.Visible = true;
            }
            else
            {
                //Kiem tra username pass word
                ReturnValue<bool> LoginResult = NguoiDungBO.getInstance.Login(Username, Pwd);
                if (LoginResult.RetValue != true)
                {
                    lblError.Text = LoginResult.Msg;
                    lblError.Visible = true;
                    txtPwd.Text = "";
                }
                else
                {
                    if(_parentForm != null)
                    {
                        _parentForm.Show();
                        _parentForm.Focus();
                        this.txtPwd.Text = "";
                        lblError.Text = "";
                        lblError.Visible = false;
                        this.Hide();
                    }
                    else
                    {
                        frmMain obj = new frmMain(this);
                        frmMain.CurrentNguoiDung = NguoiDungBO.getInstance.GetNguoiDungByUsername(Username);
                        this.txtPwd.Text = "";
                        lblError.Text = "";
                        lblError.Visible = false;
                        this.Hide();
                        obj.Show();
                        obj.Focus();
                    }
                }
            }
        }
    }
}
